﻿using Jesai.DynamicDataSource.Sqlsugar.DbConn;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace Jesai.DynamicDataSource.Sqlsugar.ConnManagers
{
    public class DBConnectionManager
    {
        // 线程安全的连接集合，key 可用 Name 或自定义唯一标识
        private readonly static ConcurrentDictionary<string, DBConnection> _connections = new();

        // 查询
        public static bool TryGet(string name, out DBConnection connection)
        {
            return _connections.TryGetValue(name, out connection);
        }

        // 新增（如果已存在则返回 false）
        public static bool Add(string name, DBConnection connection)
        {
            return _connections.TryAdd(name, connection);
        }

        // 删除
        public static bool Remove(string name)
        {
            return _connections.TryRemove(name, out _);
        }

        // 获取所有连接
        public static IEnumerable<DBConnection> GetAll()
        {
            return _connections.Values;
        }
    }
}